##
#   Project: BlueDUN
#            Internet connection manager for bluetooth devices.
#    Author: Fabio Castelli <muflone@vbsimple.net>
# Copyright: 2009 Fabio Castelli
#   License: GPL-2+
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 2 of the License, or (at your option)
#  any later version.
# 
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
#  more details.
# 
# On Debian GNU/Linux systems, the full text of the GNU General Public License
# can be found in the file /usr/share/common-licenses/GPL-2.
##

import gtk
import gtk.glade
import handlepaths
import btsupport
import gettext
from gettext import gettext as _

# Load domain for translation
for module in (gettext, gtk.glade):
  module.bindtextdomain(handlepaths.APP_NAME, handlepaths.getPath('locale'))
  module.textdomain(handlepaths.APP_NAME)

tvwServices = None
modelServices = None

def findDUNService(address):
  "Find DUN (0x1103) service"
  services = btsupport.listServices(address)
  for service in services:
    for serviceclass in service[3]:
      # Dial-up networking
      if serviceclass == '1103':
        return service

def createServicesColumns():
  "Create columns for services list"
  # Column for service name
  cell = gtk.CellRendererText()
  column = gtk.TreeViewColumn(_('Service name'))
  column.pack_start(cell)
  column.set_attributes(cell, text=0)
  tvwServices.append_column(column)

  # Column for service protocol
  cell = gtk.CellRendererText()
  column = gtk.TreeViewColumn(_('Protocol'))
  column.pack_start(cell)
  column.set_attributes(cell, text=1)
  tvwServices.append_column(column)

  # Column for service channel
  cell = gtk.CellRendererText()
  column = gtk.TreeViewColumn(_('Channel'))
  column.pack_start(cell)
  column.set_attributes(cell, text=2)
  tvwServices.append_column(column)

def showWindow(address):
  "Show services dialog"
  global tvwServices
  global modelServices

  gladeFile = gtk.glade.XML(
    fname=handlepaths.getPath('ui', 'services.glade'),
    domain=handlepaths.APP_NAME)
  gw = gladeFile.get_widget

  # Services dialog
  dialogServices = gw('dialogServices')
  dialogServices.set_icon_from_file(handlepaths.getPath('data', 'cel.svg'))
  dialogServices.set_title(_('Available services'))
  tvwServices = gw('tvwServices')

  # Model for services (name, protocol, port)
  modelServices = gtk.ListStore(str, str, str)
  tvwServices.set_model(modelServices)
  createServicesColumns()

  # Load the list of enabled services
  services = btsupport.listServices(address)
  for service in services:
    modelServices.append([service[0], service[1], service[2]])
  dialogServices.run()
  dialogServices.destroy()
